//
//  NSString.m
//  iOSBaseFramework
//
//  Created by user on 2020/6/15.
//  Copyright © 2020 com.taylor. All rights reserved.
//

#import "NSString+tool.h"
#import <UIKit/UIKit.h>

@implementation NSString (tool)

- (BOOL) isEmptyString
{
    if (self == nil) {
        return YES;
    }
    
    if ((NSNull *)self == [NSNull null]) {
        return YES;
    }
    
    
    if (self.length == 0) {
        return YES;
    }
    
    if ([self isEqualToString:@"<null>"]) {
        return YES;
    }
         
    return NO;
}

+ (NSString *)getDeviceIdentifierForVendor
{
    return [[UIDevice currentDevice].identifierForVendor UUIDString];
}

+ (NSString *) getAppVersions
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    //app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return app_Version;
}


+ (NSString *) getWeekDay:(NSTimeInterval)time
{
    //创建一个星期
    NSArray *weekDay = [NSArray arrayWithObjects:[NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    //新日期
    NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:time];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:newDate];
    NSString *weekStr = [weekDay objectAtIndex:components.weekday];
    return weekStr;
}

+ (NSString *)format:(NSTimeInterval)time
{
    if (time < 0) {
        return @"";
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000];
    return [formatter stringFromDate:date];
}

+ (NSString *)formatTime:(NSTimeInterval)time
{
    if (time < 0) {
        return @"";
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000];
    return [formatter stringFromDate:date];
}

+ (NSString *)formatDateAndTime:(NSTimeInterval)time
{
    if (time < 0)
    {
        return @"";
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000];
    return [formatter stringFromDate:date];
}


-(RangeFormatType)checkRange:(NSRange)range
{
    NSInteger *loc = range.location;
    NSInteger *len = range.length;
    
    if (loc>=0 && len>0)
    {
        if (range.location + range.length <= self.length) {
            return RangeCorrect;
        } else {
            NSLog(@" The Range out-of-bounds!");
            return RangeOut;
        }
    }
    else
    {
        NSLog(@"The Range format is wrong: NSMakeRange(a, b) (a >= 0, b > 0).");
        return RangeError;
    }
}

-(NSMutableAttributedString *)changeColor:(id)color andRange:(NSRange)range
{
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:self];
    if ([self checkRange:range] == RangeCorrect) {
        if (color) {
            [attributeStr addAttribute:NSForegroundColorAttributeName value:color range:range];
        }
        else
        {
            NSLog(@"color is nil");
        }
    }
    return attributeStr;
}


-(NSMutableAttributedString *)changeFont:(id)font andRange:(NSRange)range
{
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:self];
    if ([self checkRange:range] == RangeCorrect)
    {
        if (font) {
            [attributeStr addAttribute:NSFontAttributeName value:font range:range];
        } else {
            NSLog(@"font is nil");
//            MyLog(@"font is nil");
        }
    }
    return attributeStr;
}

-(NSMutableAttributedString *)changeColor:(id)color andColorRang:(NSRange)colorRange andFont:(id)font andFontRange:(NSRange)fontRange
{
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:self];
    if ([self checkRange:colorRange] == RangeCorrect)
    {
        if (color) {
            [attributeStr addAttribute:NSForegroundColorAttributeName value:color range:colorRange];
        } else {
            NSLog(@"NSString + tool   color is Nil");
        }
    }
    
    if ([self checkRange:fontRange] == RangeCorrect)
    {
        if (font){
            [attributeStr addAttribute:NSFontAttributeName value:font range:fontRange];
        } else {
            NSLog(@"NSString + tool  font is Nil");
        }
    }
    return attributeStr;
}





- (NSString *)decimalPrecision:(NSUInteger)precision
{
    if (precision <= 0)
    {
        return self;
    }
    
    NSString *numStr = [NSString stringWithFormat:@"%@", self];
    if ([numStr rangeOfString:@"."].location == NSNotFound)
    {
        NSMutableString *strRet = [NSMutableString stringWithFormat:@"."];
        for (int i = 0; i < precision; i++) {
            [strRet appendString:@"0"];
        }
        return strRet;
    } else {
        NSArray *array = [numStr componentsSeparatedByString:@"."];
        NSString *strInt = [array objectAtIndex:0];
        NSString *decimal = [array objectAtIndex:1];
        
        if (decimal.length > precision) {
            return [NSMutableString stringWithFormat:@"%@.%@", strInt, [decimal substringWithRange:NSMakeRange(0, precision)]];
        } else if (decimal.length < precision){
            NSMutableString *strRet = [NSMutableString stringWithFormat:@"%@.%@", strInt, decimal];
            for (int i = 0; i < precision - decimal.length; i++) {
                [strRet appendString:@"0"];
            }
            return strRet;
        } else {
            return numStr;
        }
    }
}

- (NSMutableAttributedString *)addCenterLine
{
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:self attributes:attribtDic];
    return attributeStr;
}

- (NSMutableAttributedString *)addDownLine
{
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:self attributes:attribtDic];
    return attributeStr;
}

@end
